#!/usr/bin/env python
#
# Pyhmon - node

import threading
import asyncore
import time
import signal
import sys

from tools import config
config.init()

from tools import ringbuffer
from tools import collect
from tools import alerts

exec "from net." + config.getNetBackend() + " import *"

pl_list=config.getPluginsToBuffer()
pl_and_rb=[[],[],[],[]]

i = 0
while len (pl_list)>i:
	rb = ringbuffer.RingBuffer (config.getMaxBuf (pl_list[0]))
	pl_and_rb[0].append (pl_list[i])
	i += 1
	pl_and_rb[1].append (rb)
	pl_and_rb[2].append (config.getCollectInterval(pl_and_rb[0][-1]))
	pl_and_rb[3].append (threading.Condition ())

def handle_signal (signum, frame) :
	sys.exit ("Terminating.")

def run():
	signal.signal (signal.SIGTERM, handle_signal)
	signal.signal (signal.SIGINT, handle_signal)
	conn = Conn (pl_and_rb)
	coll = collect.CollThread (pl_and_rb)
	coll.start ()
	al = alerts.AlertThread ()
	al.start ()
	asyncore.loop ()
	coll.join ()
	al.join ()

if __name__ == "__main__":
	run ()
